<template>
  <view class="category">
    <Yg-search></Yg-search>
    <view class="main">
      <view class="menus">
        <view
          :class="['item', index === current ? 'active' : '']"
          v-for="(item, index) in menus"
          :key="item"
          @click="handleClick(index)"
          >{{ item }}</view
        >
      </view>
      <scroll-view :scroll-top="scrollTop" scroll-y class="goods">
        <view
          class="goods-group"
          v-for="item1 in goodsList"
          :key="item1.cat_id"
        >
          <view class="goods-group-title">{{ item1.cat_name }}</view>
          <view class="goods-group-content">
            <navigator
              :url="`/pages/goods_list/goods_list?cid=` + item2.cat_id"
              class="goods-item"
              v-for="item2 in item1.children"
              :key="item2.cat_id"
            >
              <image :src="item2.cat_icon"></image>
              <view class="goods-name">{{ item2.cat_name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 接口的返回数据 完整！！！
      categoryList: [],

      // 左侧要渲染的标题数组
      menus: [],

      // 右侧要显示的内容数据
      goodsList: [],

      // 选中标题的索引
      current: 0,
    };
  },
  async onLoad() {
    //判断一下有没有缓存
    const category = uni.setStorageSync("category");

    if (!category) {
      //没有缓存数据就发送请求获取数据
      this.getCategory();
      // console.log("没有缓存数据就发送请求获取数据");
    } else {
      if (Date.now() - category.time > 1000 * 10) {
        //有缓存判断  过期时间10秒 后重新发送请求
        this.getCategory();
        // console.log("超过10秒");
      } else {
        //没有过期就用缓存数据
        this.setCategory(category.data);
        // console.log("没有超过10秒");
      }
    }

    const { message } = await uni.$u.http.get("/categories");
    this.setCategory(message);
  },
  methods: {
    //发送请求
    async getCategory() {
      const { message } = await uni.$u.http.get("/categories");
      this.setCategory(message);
      // console.log(82, message);
    },

    // 封装页面要用到的数据
    async setCategory(message) {
      this.categoryList = message;

      // 构造左侧标题数组
      this.menus = this.categoryList.map((v) => v.cat_name);

      // 构造右侧要显示的商品数据
      this.goodsList = this.categoryList[this.current].children;

      // 把数据存入到本地存储
      uni.setStorageSync("category", {
        data: this.categoryList, //发送请求后获取的数据
        time: Date.now(), //储存到本地的时间
      });
    },

    // 左侧标题的点击
    handleClick(index) {
      this.current = index;
      this.goodsList = this.categoryList[index].children;

      // 设置右侧滚动容器的距离
      // 每次点击的时候都生成一个无限接近于0的不重复的值
      let scrollTop =
        Math.random() *
        Math.random() *
        Math.random() *
        Math.random() *
        Math.random() *
        Math.random();

      if (this.scrollTop === scrollTop) {
        scrollTop = scrollTop / 10;
      }
      this.scrollTop = scrollTop;
    },
  },
};
</script>

<style lang="scss">
.category {
  /* 小程序中 100vh 自动帮我们去除掉 tabbar的高度！！ 也去除掉顶部的高度  */
  height: 100vh;
  display: flex;
  flex-direction: column;
  .main {
    flex: 1;
    display: flex;
    overflow: hidden;
    .menus {
      width: 182rpx;
      overflow: auto;
      .item {
        height: 80rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
        color: #666;
      }
      .active {
        color: #eb4450;
        border-left: 1rpx solid #eb4450;
      }
    }
    .goods {
      flex: 1;
      overflow: auto;
    }
  }
}

.goods {
  .goods-group {
    .goods-group-title {
      height: 80rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #666;
      font-size: 28rpx;
      &::before {
        content: "/";
        margin-right: 30rpx;
        color: #ccc;
      }
      &::after {
        color: #ccc;
        content: "/";
        margin-left: 30rpx;
      }
    }

    .goods-group-content {
      display: flex;
      flex-wrap: wrap;
      .goods-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 33.33%;
        image {
          width: 120rpx;
          height: 120rpx;
          background-color: #ccc;
        }
        .goods-name {
          color: #666;
          font-size: 28rpx;
        }
      }
    }
  }
}
</style>